//
// Created by Whg on 2019-09-23.
//


#include <cstring>
#include "AudioChannel.h"
#include "faac.h"
#include "librtmp/rtmp.h"
#include "macro.h"

//不断调用 编码
void AudioChannel::encodeData(int8_t *data) {

    int bytelen = faacEncEncode(audioCodec, reinterpret_cast<int32_t *>(data), inputSamples, buffer,
                                maxOutputBytes);

    if (bytelen > 0) {
        LOGE("音频数据");
        RTMPPacket *packet = new RTMPPacket;
        int bodySize = 2 + bytelen;
        RTMPPacket_Alloc(packet, bodySize);
        packet->m_body[0] = 0xAF;
        if (mChannels == 1) {
            packet->m_body[0] = 0xAE;
        }

        packet->m_body[1] = 0x00;

        //编码之后的aac数据
        memcpy(&packet->m_body[2], buffer, bytelen);

        //aac
        packet->m_hasAbsTimestamp = 0;
        packet->m_nBodySize = bodySize;
        packet->m_packetType = RTMP_PACKET_TYPE_AUDIO;
        packet->m_nChannel = 0x11;
        packet->m_headerType = RTMP_PACKET_SIZE_LARGE;

        audioCallback(packet);

    }
}

//初始化
void AudioChannel::setAudioEncInfo(int samplesInHZ, int channels) {
    //1最小缓冲大小
    audioCodec = faacEncOpen(samplesInHZ, channels, &inputSamples, &maxOutputBytes);
    //设置参数
    faacEncConfigurationPtr config = faacEncGetCurrentConfiguration(audioCodec);
    config->mpegVersion = MPEG4;
    //lc标准
    config->aacObjectType = LOW;
    //16位
    config->inputFormat = FAAC_INPUT_16BIT;
    //编码出原始数据，
    config->outputFormat = 0;

    faacEncSetConfiguration(audioCodec, config);

    //设置缓冲区
    buffer = new u_char[maxOutputBytes];
}


int AudioChannel::getInputSamples() {
    return inputSamples;
}

void AudioChannel::setAudioCallback(AudioChannel::AudioCallback audioCallback) {
    this->audioCallback = audioCallback;
}

RTMPPacket *AudioChannel::getAudioTag() {
    u_char *buf;
    u_long len;
    //编码器信息  解码
    faacEncGetDecoderSpecificInfo(audioCodec, &buf, &len);
    int bodySize = 2 + len;

    RTMPPacket *packet = new RTMPPacket;
    RTMPPacket_Alloc(packet, bodySize);
    packet->m_body[0] = 0xAF;
    if (mChannels == 1) {
        packet->m_body[0] = 0xAE;
    }

    packet->m_body[1] = 0x00;

    //编码之后的aac数据
    memcpy(&packet->m_body[2], buf, len);

    //aac
    packet->m_hasAbsTimestamp = 0;
    packet->m_nBodySize = bodySize;
    packet->m_packetType = RTMP_PACKET_TYPE_AUDIO;
    packet->m_nChannel = 0x11;
    packet->m_headerType = RTMP_PACKET_SIZE_LARGE;

    return packet;
}
